package com.example.config;

import java.io.Serializable;

public class ResponseMessage<T> implements Serializable {
    private static final long serialVersionUID = 8992436576262574064L;
    private int code;
    protected String message;
    protected T result;
    protected final Long timestamp = System.currentTimeMillis();

    public static <T> ResponseMessage<T> ok() {
        return (ResponseMessage<T>)ok((Object)null);
    }

    public static <T> ResponseMessage<T> ok(T result) {
        return (ResponseMessage<T>) builder().result(result).message("请求成功").code(200).build();
    }


    public static <T> ResponseMessage<T> error(String message) {
        return error(500, message);
    }

    public static <T> ResponseMessage<T> error(Integer code, String message) {
        return (ResponseMessage<T>)builder().message(message).code(code).build();
    }

    public ResponseMessage<T> result(T result) {
        this.result = result;
        return this;
    }

    public static <T> ResponseMessageBuilder<T> builder() {
        return new ResponseMessageBuilder();
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getResult() {
        return this.result;
    }

    public Long getTimestamp() {
        return this.timestamp;
    }

    public void setCode(final int code) {
        this.code = code;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public void setResult(final T result) {
        this.result = result;
    }

    public ResponseMessage(final int code, final String message, final T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public ResponseMessage() {
    }

    public static class ResponseMessageBuilder<T> {
        private int code;
        private String message;
        private T result;

        ResponseMessageBuilder() {
        }

        public ResponseMessageBuilder<T> code(final int code) {
            this.code = code;
            return this;
        }

        public ResponseMessageBuilder<T> message(final String message) {
            this.message = message;
            return this;
        }

        public ResponseMessageBuilder<T> result(final T result) {
            this.result = result;
            return this;
        }

        public ResponseMessage<T> build() {
            return new ResponseMessage(this.code, this.message, this.result);
        }

        public String toString() {
            return "ResponseMessage.ResponseMessageBuilder(code=" + this.code + ", message=" + this.message + ", result=" + this.result + ")";
        }
    }
}
